/* Copyright © 2006 Sarp Arda Coskun.
* Contact e-mail: sacoskun@gmail.com

* This file is part of simpleP2P.

* simpleP2P is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License

* simpleP2P is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with AndPishti.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * Client Thread
 * Client program calls these threads in order to
 * listen uploads.

 * Author: Sarp Arda Coskun - 20201238
 * Date: 18 October 2006 with Eclipse 3.1
 * Version: 0.1 (Beta)
 * Tested on : 20 October 2006 with SuSE 10.0 Linux
 */

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.StringTokenizer;

public class ClientThread implements Runnable
{
	Socket connectionSocket;			// Port listens any transfer request from connected clients.

	public ClientThread(Socket socket)
	{
		connectionSocket = socket;
	}
	public void run()
	{
		try
		{
			// Input and output streams.
			BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
			DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
			
			// Server is waiting input from client.
			System.out.println("Client is waiting input for client");
			StringTokenizer clientSentenceST = new StringTokenizer(inFromClient.readLine());
			System.out.println("Client recieved input.");
			
			if(clientSentenceST.hasMoreTokens())
			{
				// Checks the first token of command.
				String clientCommandToken = clientSentenceST.nextToken();
				if(clientCommandToken.equals("SEND"))
				{				
					// Uploads the file to the client.
					String fileNameToSend = clientSentenceST.nextToken();								
					if(!clientSentenceST.hasMoreTokens())
					{
						// One whole file.
						File fileToSend = new File("shared" + File.separator + fileNameToSend );
						FileReader fileReaderToSend = new FileReader(fileToSend);
						int currentByte = fileReaderToSend.read();
						String byteArray = "";
						
						while(currentByte != -1)
						{
							byteArray += currentByte + " ";
							currentByte = fileReaderToSend.read();
						}
						fileReaderToSend.close();
						byteArray = byteArray.substring(0, byteArray.length() - 1);	// Deletes the last " " space.
						outToClient.writeBytes("FILE " + fileNameToSend + "\n\n" + byteArray + "\n");
						System.out.println(byteArray);
					}
					else
					{
						int lowerBound = Integer.parseInt(clientSentenceST.nextToken());
						int upperBound = Integer.parseInt(clientSentenceST.nextToken());
						System.out.println("lower:" + lowerBound + " upper:" + upperBound);
						
						File fileToSend = new File("shared" + File.separator + fileNameToSend );
						FileReader fileReaderToSend = new FileReader(fileToSend);
						int currentByte = fileReaderToSend.read();
						String byteArrayToSend = "";
						int i = 0;
						while(currentByte != -1)
						{
							if(i < upperBound && i >= lowerBound)
							{
								byteArrayToSend += currentByte + " ";
							}
							currentByte = fileReaderToSend.read();
							i++;
						}
						fileReaderToSend.close();
						byteArrayToSend = byteArrayToSend.substring(0, byteArrayToSend.length()-1);	// Deletes the last " " space.
						
						// Splitted part of the file.
						outToClient.writeBytes("FILE " + fileNameToSend + "\n\n" + byteArrayToSend + "\n");

						System.out.println("SPLIT FILE:" + byteArrayToSend);

					}
				}
				else
				{
					System.out.println("Wrong command received.");
				}
			}
		}
		catch (Exception e)
		{
			System.err.print(e);
		}
		
	}
	
}

